package String._809_ExpressiveWords;

public class _809_ExpressiveWords {
    public int expressiveWords(String S, String[] words) {
        RLE R = new RLE(S);
        int count = 1;
        int result = 0;
        for (String word:words) {
            RLE tempR = new RLE(word);
            boolean findOut = true;
            if (!tempR.key.equals(R.key)) {
                count++;
                continue;
            }
            int N = R.counts.size();
            for (int i = 0; i < N; i++) {
                int count1 = R.counts.get(i);
                int count2 = tempR.counts.get(i);
                if (count1 < count2 || (count1 < 3 && count1 != count2)) {
                    findOut = false;
                    break;
                }
            }
            if (findOut) {
                result = count;
            }
            count++;
        }
        return result;
    }
}
